import { queryCurUser } from '../../apis/account'
import { queryList as queryUsers } from '../../apis/user'
import { queryList as queryGroups } from '../../apis/group'

/**
 * 查询当前用户
 */
export const queryCurUserAction = (callback) => {
  return async (dispatch) => {
    try {
      const data = await queryCurUser()
      if (data.id) {
        dispatch({
          type: 'SET_CUR_USER',
          data: data
        })
      } else {
        throw '您未登录'
      }
    } catch (error) {
      throw error
    }
  }
}

// 查询用户列表
export const queryUsersAction = () => {
  return async (dispatch) => {
    const { data } = await queryUsers()
    dispatch({
      type: 'SET_USERS',
      data: data
    })
  }
}

// 查询用户组列表
export const queryUserGroupsAction = () => {
  return async (dispatch) => {
    const data = await queryGroups()
    dispatch({
      type: 'SET_USER_GROUPS',
      data: data
    })
  }
}